﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using WMSmanagement.IBaseService.Transfer;
using WMSmanagement.IBaseService.Transfer.DTO;
using WMSmanagement.Model;

namespace WMSmanagement.BaseService
{
    [ApiExplorerSettings(GroupName = "刘海通")]

    public class TransferService : ApplicationService, ITransferService
    {
        private readonly IRepository<Transfer, int> _repository;
        private readonly IRepository<TransferDetails, int> _TransferDetailsrepository;
        private readonly IRepository<Products, int> _Productsrepository;

        public TransferService(IRepository<Transfer, int> repository, IRepository<TransferDetails, int> transferDetailsrepository, IRepository<Products, int> productsrepository)
        {
            _repository = repository;
            _TransferDetailsrepository = transferDetailsrepository;
            _Productsrepository = productsrepository;
        }
        /// <summary>
        /// 查询移库表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<GetTransfer> GetTransferAsync(RequsetTransfer model)
        {
            var list = from A in await _repository.GetListAsync()
                       join B in await _TransferDetailsrepository.GetListAsync()
                       on A.TransferCode equals B.TransferCode
                       join C in await _Productsrepository.GetListAsync()
                       on B.ProductCode equals C.ProductCode
                       select new ShowTransferDTO
                       {
                            Id=A.Id,
                            TransferCode=A.TransferCode,
                            TransferType=A.TransferType,
                            Maker=A.Maker,
                            MakeTime=A.MakeTime,
                            Remark=A.Remark,
                            ProductCode=C.ProductCode,
                            ProductName=C.ProductName,
                            ProductSpecification=C.ProductSpecification,
                            Category=C.Category,
                            StorageUnit=C.StorageUnit,
                            Unit=C.Unit,
                            UnitPrice=C.UnitPrice,
                           Quantity=B.Quantity,

                       };
            list = list.WhereIf(!string.IsNullOrEmpty(model.TransferCode), c => c.TransferCode.Contains(model.TransferCode) );
            list = list.WhereIf(model.TransferType != "0", c => c.TransferType == model.TransferType);
            var listcount=list.Count();
            list=list.Skip((model.pageindex-1)*model.pagesize).Take(model.pagesize);
            if(listcount==0)
            {
                list=new List<ShowTransferDTO>();
            }
            return new GetTransfer
            {
                list = list.ToList(),
                listcount = listcount,
                code = 0
            };
        }
        /// <summary>
        /// 添加移库表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<bool> AddTransferAsnyc(AddTransferDTO model)
        {
            var list = model.MapTo<Transfer>();
            var result1=await _repository.InsertAsync(list)!=null;
            var detilList = new List<TransferDetails>();
            foreach (var item in model.ids)
            {
                var detil = new TransferDetails()
                {
                    TransferCode=model.TransferCode,
                    ProductCode=item.Id,
                    Quantity=item.Num,
                };
                detilList.Add(detil);
            }
             await _TransferDetailsrepository.InsertManyAsync(detilList);
            return result1;
        }
        /// <summary>
        /// 修改移库表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public  async Task<bool> UpdateTransferAsnyc(UpdateTransferDTO model)
        {
            var list = model.MapTo<Transfer>();
            var result=await _repository.UpdateAsync(list)!=null;
            var DelList=await _TransferDetailsrepository.GetListAsync(c=>c.TransferCode==model.TransferCode);
            await _TransferDetailsrepository.DeleteManyAsync(DelList);
            var detilList = new List<TransferDetails>();
            foreach (var item in model.ids)
            {
                var detil = new TransferDetails()
                {
                    TransferCode = model.TransferCode,
                    ProductCode = item.Id,
                    Quantity = item.Num,
                };
                detilList.Add(detil);
            }
            await _TransferDetailsrepository.InsertManyAsync(detilList);
            return result;
        }
        /// <summary>
        /// 删除移库表
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<bool> DeleteTransferAsnyc(string id)
        {
            var transferDeyil = await _TransferDetailsrepository.FindAsync(c => c.ProductCode == id);
            await _TransferDetailsrepository.DeleteAsync(transferDeyil);
            var count = await _TransferDetailsrepository.CountAsync(c => c.TransferCode == transferDeyil.TransferCode);
            if (count == 0)
            {
                await _repository.DeleteAsync(c => c.TransferCode == transferDeyil.TransferCode);
            }

            return true;
        }
    }
}
